<!--
 * @Author: your name
 * @Date: 2022-04-27 09:44:49
 * @LastEditTime: 2022-04-27 10:25:56
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \算法测试\day0906\417.水流问题.html
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /**
 * @param {number[][]} heights
 * @return {number[][]}
 */
        var pacificAtlantic = function (heights) {
            let m = heights.length, n = heights[0].length;
            let step = [[-1, 0], [0, 1], [1, 0], [0, -1]];
            let arr = Array(m).fill(0).map(() => { return Array(n).fill([0, 0]) });
            let result = [];
            let left = 0;
            const dfs = function (x, y) {
                arr[x][y][left] = 1;
                let num = heights[x][y];
                heights[x][y] = -1;
                for (let i = 0; i < step.length; i++) {
                    let newx = x + step[i][0];
                    let newy = y + step[i][1];
                    if (newx >= 0 && newy >= 0 && newx < m && newy < n && heights[newx][newy] >= num && heights[newx][newy] !== -1) dfs(newx, newy);
                }
                heights[x][y] = num;
            }
            for (let i = 0; i < m; i++) {
                if (i === 0) {
                    for (let j = 0; j < n; j++) {
                        dfs(i, j);
                    }
                } else {
                    dfs(i, 0);
                }
            }
            left = 1;
            for (let i = 0; i < m; i++) {
                if (i === m - 1) {
                    for (let j = 0; j < n; j++) {
                        dfs(i, j);
                    }
                } else {
                    dfs(i, n - 1);
                }
            }
            for (let i = 0; i < m; i++) {
                for (let j = 0; j < n; j++) {
                    if (arr[i][j][0] && arr[i][j][1]) result.push([i, j]);
                }
            }
            return arr;
        };
        console.log(pacificAtlantic([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], [52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 15], [51, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 65, 16], [50, 95, 132, 133, 134, 135, 136, 137, 138, 139, 140, 107, 66, 17], [49, 94, 131, 160, 161, 162, 163, 164, 165, 166, 141, 108, 67, 18], [48, 93, 130, 159, 180, 181, 182, 183, 184, 167, 142, 109, 68, 19], [47, 92, 129, 158, 179, 192, 193, 194, 185, 168, 143, 110, 69, 20], [46, 91, 128, 157, 178, 191, 196, 195, 186, 169, 144, 111, 70, 21], [45, 90, 127, 156, 177, 190, 189, 188, 187, 170, 145, 112, 71, 22], [44, 89, 126, 155, 176, 175, 174, 173, 172, 171, 146, 113, 72, 23], [43, 88, 125, 154, 153, 152, 151, 150, 149, 148, 147, 114, 73, 24], [42, 87, 124, 123, 122, 121, 120, 119, 118, 117, 116, 115, 74, 25], [41, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 26], [40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27]]));
    </script>
</body>

</html>